﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Program18
{
    public partial class Form1 : Form
    {
        Thread thread;
        public Form1()
        {
            InitializeComponent();

            //取消跨线程错误检测
            CheckForIllegalCrossThreadCalls = false;

            thread = new Thread(new ThreadStart(Secend));
        }
        private void Secend()
        {
            for (int i = 0; i < 1000000; i++)
            {
                Thread.Sleep(1000);
                txtTime.Text = i.ToString();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //判断线程是否正在运行
            if (thread.IsAlive)
            {
                thread.Abort();
            }

            //创建线程调用计时方法
            thread = new Thread(Secend);

            //将线程设为后台线程
            thread.IsBackground = true;

            //开始线程
            thread.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thread.IsAlive)
            {
                thread.Abort();
            }
        }
    }
}
